<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" media-type="text/plain"/>
	<xsl:param name="MySubjectArea"/>
	<xsl:template match="/">
		<busMatrix>
			<xsl:variable name="SubjectAreaID" select="//PresentationCatalog[@name = $MySubjectArea]/@id"/>
			<xsl:variable name="BusinessModelID" select="//PresentationCatalog[@name = $MySubjectArea]/RefBusinessModel/@id"/>
			<xsl:comment>Subject Area section</xsl:comment>
			<!-- Select a subject area with a matching ID -->
			<xsl:copy-of select="//PresentationCatalog[@id = $SubjectAreaID]"/>
			<!-- Select all presentation tables and columns for presentation tables in this subject area -->
			<xsl:for-each select="//PresentationTable[@parentId = $SubjectAreaID]">
				<xsl:copy-of select="."/>
				<xsl:variable name="PresentationTableID" select="@id"/>
				<xsl:copy-of select="//PresentationColumn[@parentId = $PresentationTableID]"/>
			</xsl:for-each>
			<xsl:comment>Business Model section</xsl:comment>
			<!-- Selecting the Business Model for the subject area -->
			<xsl:copy-of select="//BusinessModel[@id = $BusinessModelID]"/>
			<!-- copying all logical tables and columns in that business model -->
			<xsl:for-each select="//LogicalTable[@parentId = $BusinessModelID]">
				<xsl:copy-of select="."/>
				<xsl:variable name="LogicalTableID" select="@id"/>
				<!-- copying all logical table sources and column mappings -->
				<xsl:for-each select="//LogicalTableSource[@parentId = $LogicalTableID]">
					<xsl:copy-of select="."/>
					<xsl:variable name="LogicalTableSourceID" select="@id"/>
					<xsl:copy-of select="//ColumnMapping[./RefLogicalTableSource/@id = $LogicalTableSourceID]"/>
				</xsl:for-each>
				<xsl:for-each select="//LogicalColumn[@parentId = $LogicalTableID]">
					<xsl:copy-of select="."/>
					<xsl:variable name="LogicalColumnID" select="@id"/>
					<!-- copying Measure Definitions and Overriding Aggregate Rules -->
					<xsl:for-each select="//MeasureDefn[@parentId = $LogicalColumnID]">
						<xsl:copy-of select="."/>
						<xsl:variable name="MeasureDefinitionID" select="@id"/>
						<xsl:copy-of select="//OverrideAggrRule[@parentId = $MeasureDefinitionID]"/>
					</xsl:for-each>
				</xsl:for-each>
			</xsl:for-each>
			<!-- copying all hierarchies, levels and logical keys -->
			<xsl:for-each select="//Dimension[@parentId = $BusinessModelID]">
				<xsl:copy-of select="."/>
				<xsl:variable name="DimensionID" select="@id"/>
				<xsl:for-each select="//LogicalLevel[@parentId = $DimensionID]">
					<xsl:copy-of select="."/>
					<xsl:variable name="LogicalLevelID" select="."/>
					<xsl:copy-of select="//LogicalKey[@parentId = $LogicalLevelID]"/>
				</xsl:for-each>
			</xsl:for-each>
			<!-- copying all logical joins -->
			<xsl:copy-of select="//LogicalComplexJoin"/>
			<!-- copying all physical objects -->
			<!-- we hope... -->
		</busMatrix>
	</xsl:template>
</xsl:stylesheet>

